/*
 * @lc app=leetcode.cn id=216 lang=cpp
 *
 * [216] 组合总和 III
 */

// @lc code=start
class Solution
{
public:
  vector<vector<int>> ans;

  vector<vector<int>> combinationSum3(int k, int n)
  {
    vector<int> tmp;
    Backtracking(k, n, 0, 0, tmp);
    return ans;
  }

  void Backtracking(int k, int n, int sum, int start, vector<int>& tmp)
  {
    if (sum > n || tmp.size() > k)
      return;
    if (sum == n && tmp.size() == k)
      ans.push_back(tmp);
    else {
      for (int i = start + 1; i <= 9; ++i) {
        tmp.push_back(i);
        Backtracking(k, n, sum + i, i, tmp);
        tmp.pop_back();
      }
    }
  }
};
// @lc code=end
